/*
 * Copyright (C) 2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

/*
 * Prototypes
 */
#if PROTO
static void
cp_abs_cp(c_t *u, c_t *v, eint32_t m);

static void
sp_abs_sp(s_t *u, s_t *v, eint32_t m);

static void
ip_abs_ip(i_t *u, i_t *v, eint32_t m);

static void
lp_abs_lp(l_t *u, l_t *v, eint32_t m);

static void
fp_abs_fp(f_t *u, f_t *v, eint32_t m);

static void
dp_abs_dp(d_t *u, d_t *v, eint32_t m);

static void
vo_abs_vo(vo_t *u, vo_t *v, eint32_t m);
#endif

/*
 * Implementation
 */
#if CODE
void
evector_abs(evector_t *v, etype_t t)
{
    eint32_t	 l;
    evector_t	*r;
    etype_t	 rt;
    v_enter();

    v_check(2);
    l = v->length;

    rt = match(t, t, 0, 0);
    if (rt != t)
	v = coerce(v, t, rt);
    r = create(v, rt);

    switch (rt) {
	case t_int8:
	    cp_abs_cp(r->v.obj, v->v.obj, l);
	    break;
	case t_int16:
	    sp_abs_sp(r->v.obj, v->v.obj, l);
	    break;
	case t_int32:
	    ip_abs_ip(r->v.obj, v->v.obj, l);
	    break;
	case t_int64:
	    lp_abs_lp(r->v.obj, v->v.obj, l);
	    break;
	case t_float32:
	    fp_abs_fp(r->v.obj, v->v.obj, l);
	    break;
	case t_float64:
	    dp_abs_dp(r->v.obj, v->v.obj, l);
	    break;
	default:
	    vo_abs_vo(r->v.obj, v->v.obj, l);
	    break;
    }
    v_leave();
}

static void
cp_abs_cp(c_t *u, c_t *v, eint32_t m)
{
    for (; m; m--, u++, v++)
	*u = *v >= 0 ? *v : -*v;
}

static void
sp_abs_sp(s_t *u, s_t *v, eint32_t m)
{
    for (; m; m--, u++, v++)
	*u = *v >= 0 ? *v : -*v;
}

static void
ip_abs_ip(i_t *u, i_t *v, eint32_t m)
{
    for (; m; m--, u++, v++)
	*u = *v >= 0 ? *v : -*v;
}

static void
lp_abs_lp(l_t *u, l_t *v, eint32_t m)
{
    for (; m; m--, u++, v++)
	*u = *v >= 0 ? *v : -*v;
}

static void
fp_abs_fp(f_t *u, f_t *v, eint32_t m)
{
    while (m--)
	*u++ = fabs(*v++);
}

static void
dp_abs_dp(d_t *u, d_t *v, eint32_t m)
{
    while (m--)
	*u++ = fabs(*v++);
}

static void
vo_abs_vo(vo_t *u, vo_t *v, eint32_t m)
{
    eint_t	 i;

    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_i(0);
		break;
	    case t_int:
		if ((i = ez_abs_i(thr_zr, get_i(v))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		set_d(ed_abs_d(get_d(v)));
		break;
	    case t_mpz:
		ez_abs_z(thr_zr, get_z(v));
		set_z(thr_zr);
		break;
	    case t_mpq:
		eq_abs_q(thr_qr, get_q(v));
		set_q(thr_qr);
		break;
	    case t_mpr:
		er_abs_r(thr_rr, get_r(v));
		set_r(thr_rr);
		break;
	    case t_cdd:
		set_dd(ed_abs_dd(get_dd(v)));
		break;
	    case t_cqq:
		if (!thread_self->flt)
		    set_dd(ed_abs_qq(get_qq(v)));
		else {
		    er_abs_qq(thr_rr, get_qq(v));
		    set_r(thr_rr);
		}
		break;
	    case t_mpc:
		er_abs_cc(thr_rr, get_cc(v));
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_number);
	}
    }
}
#endif
